package com.sun.officeoa.controller;

import com.alibaba.fastjson.JSONObject;
import com.sun.officeoa.bean.Department;
import com.sun.officeoa.bean.Employee;
import com.sun.officeoa.service.DepartmentService;
import com.sun.officeoa.service.EmployeeService;
import com.sun.officeoa.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/* ━━━━━━如来保佑━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━永无BUG━━━━━━
 * 技术大咖-SUN-JM
 * @Date: 2019/2/28 18:56
 * @Description:  TODO
 */
@Controller
@RequestMapping(value = "/office-oa")
public class RegController {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private DepartmentService departmentService;
    /**
     * @Author SUN-JM
     * @Description //TODO 员工注册
     * @Date 11:32 2019/3/1
     * @Param [request]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("doreg")
    public ModelAndView regLogin(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        String employeename = request.getParameter("employeename");
        String accountname = request.getParameter("accountname");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String deptid = request.getParameter("deptid");
        if(StringUtil.isNullOrEmpty(deptid) || StringUtil.isNullOrEmpty(accountname) || StringUtil.isNullOrEmpty(password) || StringUtil.isNullOrEmpty(employeename) ){
            modelAndView = this.reg(request,"注册失败，请填写必填信息");
            return modelAndView;
        }
        if(!StringUtil.isNullOrEmpty(email)){
            if(!email.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")){
                modelAndView = this.reg(request,"注册失败，邮箱格式不正确");
                return modelAndView;
            }
        }
        Employee employee = new Employee();
        employee.setEmployeename(employeename);
        employee.setUsername(accountname);
        employee.setEmail(email);
        employee.setPhone(phone);
        employee.setPassword(password);
        employee.setDepartmentid(Integer.parseInt(deptid));
        int reg = employeeService.insert(employee);
        if (reg == 1) {
            //注册成功，跳转到登录页面
            modelAndView.setViewName("/login");
        } else if (reg == -1) {
            //用户名重复，注册失败
            modelAndView = this.reg(request,"用户名重复，注册失败");
        } else {
            modelAndView = this.reg(request,"不明原因，注册失败");
        }
        return modelAndView;
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 注册页面部门查询
     * @Date 11:32 2019/3/1
     * @Param [request, msg]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("reg")
    public ModelAndView reg(HttpServletRequest request,String msg){
        ModelAndView modelAndView = new ModelAndView();
        if(!StringUtil.isNullOrEmpty(msg)){
            modelAndView.addObject("error",msg);
        }
        List<Department> list = departmentService.getAllDepartment();
        modelAndView.addObject("list",list);
        modelAndView.setViewName("/register");
        return modelAndView;
    }

    @RequestMapping("/getEmpByDepId")
    public void getEmpByDepId(HttpServletRequest request , HttpServletResponse resp)throws IOException{
        String depid = request.getParameter("depid");
        List<Employee> list = employeeService.getEmpByDepId(Integer.parseInt(depid));
        String jsonString = JSONObject.toJSONString(list);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(jsonString);
        resp.getWriter().close();
    }

    @RequestMapping("/getalldepjson")
    public void getalldepjson(HttpServletRequest request ,HttpServletResponse resp) throws IOException {
        List<Department> list = departmentService.getAllDepartment();
        String jsonString = JSONObject.toJSONString(list);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(jsonString);
        resp.getWriter().close();
    }


}
